package cn.danger.com.fragment.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Map;

import cn.danger.com.activity.CommonActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BackHandledFragment;
import cn.danger.com.presenter.UserPresenter;
import cn.danger.com.presenter.UserPresenterImpl;
import cn.danger.com.util.FileCacheService;

/**
 * Created by 俊 on 2016/12/30.
 */
public class ChooseColorFragment extends BackHandledFragment implements View.OnClickListener {

    private boolean hadIntercept;
    private View view;
    private CommonActivity activity;
    private Toolbar toolbar;
    private TextView color1_textView, color2_textView, color3_textView, color4_textView, color5_textView, color6_textView;
    private TextView color7_textView, color8_textView, color9_textView, color10_textView, color11_textView, color12_textView;
    private ImageView color1_imageView, color2_imageView, color3_imageView, color4_imageView, color5_imageView, color6_imageView;
    private ImageView color7_imageView, color8_imageView, color9_imageView, color10_imageView, color11_imageView, color12_imageView;
    private int position;
    private UserPresenter presenter;
    private String type;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_choose_color, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (CommonActivity) getActivity();
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle("");
        activity.setSupportActionBar(toolbar);

        color1_textView = (TextView) view.findViewById(R.id.color1_textView);
        color2_textView = (TextView) view.findViewById(R.id.color2_textView);
        color3_textView = (TextView) view.findViewById(R.id.color3_textView);
        color4_textView = (TextView) view.findViewById(R.id.color4_textView);
        color5_textView = (TextView) view.findViewById(R.id.color5_textView);
        color6_textView = (TextView) view.findViewById(R.id.color6_textView);
        color7_textView = (TextView) view.findViewById(R.id.color7_textView);
        color8_textView = (TextView) view.findViewById(R.id.color8_textView);
        color9_textView = (TextView) view.findViewById(R.id.color9_textView);
        color10_textView = (TextView) view.findViewById(R.id.color10_textView);
        color11_textView = (TextView) view.findViewById(R.id.color11_textView);
        color12_textView = (TextView) view.findViewById(R.id.color12_textView);

        color1_imageView = (ImageView) view.findViewById(R.id.color1_imageView);
        color2_imageView = (ImageView) view.findViewById(R.id.color2_imageView);
        color3_imageView = (ImageView) view.findViewById(R.id.color3_imageView);
        color4_imageView = (ImageView) view.findViewById(R.id.color4_imageView);
        color5_imageView = (ImageView) view.findViewById(R.id.color5_imageView);
        color6_imageView = (ImageView) view.findViewById(R.id.color6_imageView);
        color7_imageView = (ImageView) view.findViewById(R.id.color7_imageView);
        color8_imageView = (ImageView) view.findViewById(R.id.color8_imageView);
        color9_imageView = (ImageView) view.findViewById(R.id.color9_imageView);
        color10_imageView = (ImageView) view.findViewById(R.id.color10_imageView);
        color11_imageView = (ImageView) view.findViewById(R.id.color11_imageView);
        color12_imageView = (ImageView) view.findViewById(R.id.color12_imageView);

        Bundle bundle = getArguments();
        type = bundle.getString("type");
        FileCacheService service = new FileCacheService(activity);
        Map<String, Object> map = (Map<String, Object>) service.getSharePreference("setting");
        position = Integer.parseInt(map.get(type).toString());

        init();

        presenter = new UserPresenterImpl(activity);
    }

    @Override
    public void bindEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
        color1_textView.setOnClickListener(this);
        color2_textView.setOnClickListener(this);
        color3_textView.setOnClickListener(this);
        color4_textView.setOnClickListener(this);
        color5_textView.setOnClickListener(this);
        color6_textView.setOnClickListener(this);
        color7_textView.setOnClickListener(this);
        color8_textView.setOnClickListener(this);
        color9_textView.setOnClickListener(this);
        color10_textView.setOnClickListener(this);
        color11_textView.setOnClickListener(this);
        color12_textView.setOnClickListener(this);
    }

    private void init() {
        switch (position) {
            case 1:
                color1_imageView.setVisibility(View.VISIBLE);
                break;
            case 2:
                color2_imageView.setVisibility(View.VISIBLE);
                break;
            case 3:
                color3_imageView.setVisibility(View.VISIBLE);
                break;
            case 4:
                color4_imageView.setVisibility(View.VISIBLE);
                break;
            case 5:
                color5_imageView.setVisibility(View.VISIBLE);
                break;
            case 6:
                color6_imageView.setVisibility(View.VISIBLE);
                break;
            case 7:
                color7_imageView.setVisibility(View.VISIBLE);
                break;
            case 8:
                color8_imageView.setVisibility(View.VISIBLE);
                break;
            case 9:
                color9_imageView.setVisibility(View.VISIBLE);
                break;
            case 10:
                color10_imageView.setVisibility(View.VISIBLE);
                break;
            case 11:
                color11_imageView.setVisibility(View.VISIBLE);
                break;
            case 12:
                color12_imageView.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.modify_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.save) {
            if (type.equals("work_color")) {
                presenter.updateWorkColor(position);
            } else if (type.equals("route_color")) {
                presenter.updateRouteColor(position);
            } else if (type.equals("life_color")) {
                presenter.updateLifeColor(position);
            }
            Intent intent = new Intent("update_schedule_list");
            activity.sendBroadcast(intent);
            activity.finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }

    private void reset() {
        color1_imageView.setVisibility(View.GONE);
        color2_imageView.setVisibility(View.GONE);
        color3_imageView.setVisibility(View.GONE);
        color4_imageView.setVisibility(View.GONE);
        color5_imageView.setVisibility(View.GONE);
        color6_imageView.setVisibility(View.GONE);
        color7_imageView.setVisibility(View.GONE);
        color8_imageView.setVisibility(View.GONE);
        color9_imageView.setVisibility(View.GONE);
        color10_imageView.setVisibility(View.GONE);
        color11_imageView.setVisibility(View.GONE);
        color12_imageView.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.color1_textView:
                position = 1;
                reset();
                color1_imageView.setVisibility(View.VISIBLE);
                break;
            case R.id.color2_textView:
                position = 2;
                reset();
                color2_imageView.setVisibility(View.VISIBLE);
                break;
            case R.id.color3_textView:
                position = 3;
                reset();
                color3_imageView.setVisibility(View.VISIBLE);
                break;
            case R.id.color4_textView:
                position = 4;
                reset();
                color4_imageView.setVisibility(View.VISIBLE);
                break;
            case R.id.color5_textView:
                position = 5;
                reset();
                color5_imageView.setVisibility(View.VISIBLE);
                break;
            case R.id.color6_textView:
                position = 6;
                reset();
                color6_imageView.setVisibility(View.VISIBLE);
                break;
            case R.id.color7_textView:
                position = 7;
                reset();
                color7_imageView.setVisibility(View.VISIBLE);
                break;
            case R.id.color8_textView:
                position = 8;
                reset();
                color8_imageView.setVisibility(View.VISIBLE);
                break;
            case R.id.color9_textView:
                position = 9;
                reset();
                color9_imageView.setVisibility(View.VISIBLE);
                break;
            case R.id.color10_textView:
                position = 10;
                reset();
                color10_imageView.setVisibility(View.VISIBLE);
                break;
            case R.id.color11_textView:
                position = 11;
                reset();
                color11_imageView.setVisibility(View.VISIBLE);
                break;
            case R.id.color12_textView:
                position = 12;
                reset();
                color12_imageView.setVisibility(View.VISIBLE);
                break;
            default:
                break;

        }
    }
}
